package persistent.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import basic.HibBaseDAO;
import basic.HibernateSessionFactory;
import model.TExaminationPaper;
import model.TExaminationPaperContent;
import persistent.dao.IExaminationPaperDAO;
import util.toolkit.LogUtil;

/**
 * 试卷信息处理业务接口实现
 * @author 徐震霖
 * @since 2019-02-21
 */
public class ExaminationPaperImpl implements IExaminationPaperDAO {

	private HibBaseDAO dao = null;
	private static final Log log = LogFactory.getLog(HibBaseDAO.class);
	
	public ExaminationPaperImpl() {
		dao = new HibBaseDAO();
	}
	
	/**
	 * 添加一张试卷信息
	 * @param paper 试卷信息对象
	 * @param contents 试卷内容（题目）对象信息列表
	 * @return 成功返回true，失败返回null
	 */
	@Override
	public boolean addPaper(TExaminationPaper paper, List<TExaminationPaperContent> contents) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			int paperid = 0;
			Serializable key = session.save(paper);
			paperid = ((Integer)key).intValue();
			for (TExaminationPaperContent tExaminationPaperContent : contents) {
				tExaminationPaperContent.setPaperid(paperid);
				session.save(tExaminationPaperContent);
			}
			tx.commit();
			session.close(); //关闭连接
			return true;
		} catch (Exception e) {
			if (tx != null) tx.rollback();
			session.close(); //关闭连接
			log.error("persistent.impl.ExaminationPaperImpl类的addPaper(TExaminationPaper paper, List<TExaminationPaperContent> contents)方法调用错误：");
			log.error(LogUtil.getMsg(e));
			System.out.println("persistent.impl.ExaminationPaperImpl类的addPaper(TExaminationPaper paper, List<TExaminationPaperContent> contents)方法调用错误：");
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 删除一个试卷信息（删除试卷信息前要先删除试卷内容信息（题目））
	 * @param paperid 试卷编号
	 * @return 成功返回true，失败返回null
	 */
	@Override
	public boolean deletePaper(int paperid) {
		
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();		
			TExaminationPaper paper = 
					(TExaminationPaper) dao.findById(TExaminationPaper.class, paperid);		
			String hql = "from TExaminationPaperContent where paperid = ?";
			Object[] para = { paperid };
			List<TExaminationPaperContent> paperContents = dao.select(hql, para);
			for (TExaminationPaperContent tExaminationPaperContent : paperContents) {
				session.delete(tExaminationPaperContent);
			}			
			session.delete(paper);			
			tx.commit();
			session.close(); //关闭连接
			return true;
		} catch (Exception e) {
			if (tx != null) tx.rollback();
			session.close(); //关闭连接
			log.error("persistent.impl.ExaminationPaperImpl类的deletePaper(int paperid)方法调用错误：");
			log.error(LogUtil.getMsg(e));
			System.out.println("persistent.impl.ExaminationPaperImpl类的deletePaper(int paperid)方法调用错误：");
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 根据试卷编号得到一个试卷信息
	 * @param paperid 试卷编号
	 * @return 成功返回试卷信息对象，失败返回null
	 */
	@Override
	public TExaminationPaper getTPaper(int paperid) {
		return (TExaminationPaper) dao.findById(TExaminationPaper.class, paperid);
	}

	/**
	 * 根据条件查询所有试卷信息
	 * @param strWhere 查询条件
	 * @return 成功返回试卷对象列表，失败返回null
	 */
	@Override
	public List<TExaminationPaper> getPapers(String strWhere) {
		String hql = "from TExaminationPaper " + strWhere;
		return dao.select(hql);
	}

	/**
	 * 根据条件分页查询所有试卷信息
	 * @param strWhere 查询条件
	 * @param currentPage 当前页
	 * @param pageSize 每页条数
	 * @return 成功返回试卷对象列表，失败返回null
	 */
	@Override
	public List<TExaminationPaper> getPapersByPage(String strWhere, int currentPage, int pageSize) {
		String hql = "from TExaminationPaper " + strWhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}

	/**
	 * 根据条件得到试卷内容（题目）信息
	 * @param strWhere 查询条件
	 * @return 成功返回试卷内容对象列表，失败返回null
	 */
	@Override
	public List<TExaminationPaperContent> getContents(String strWhere) {
		String hql = "from TExaminationPaperContent " + strWhere;
		return dao.select(hql);
	}

	/**
	 * 根据条件分页得到试卷内容（题目）信息
	 * @param strWhere 查询条件
	 * @param currentPage 当前页
	 * @param pageSize 每页条数
	 * @return 成功返回试卷内容对象列表，失败返回null
	 */
	@Override
	public List<TExaminationPaperContent> getContentsByPage(String strWhere, int currentPage, int pageSize) {
		String hql = "from TExaminationPaperContent " + strWhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}

}
